<?php
/**
 * Created by PhpStorm.
 * User: zyh
 * Date: 2018/5/9
 * Time: 下午12:12
 */

namespace app\admin\controller;


use app\admin\model\Log;
use app\admin\model\Userhis;
use think\Cache;
use think\Config;
use think\Controller;
use think\Db;

class User extends Controller
{
    public function member_list()
    {
        $users = \app\admin\model\User::all();
//        print_r($users);die;
        $this->assign("count",count($users));
        $this->assign("users",$users);
        return view();
    }

    public function member_record()
    {
        $datemin = input('post.datemin','');
        $datemax = input('post.datemax','');
        $con = '';
        //子查询解决
        if ($datemin && $datemin){
            $con = ' and userhis.create_time between  "'.$datemin .' 00:00:00 "   and  "'.$datemax.' 23:59:59"    ';
        }

        $sql = "SELECT userhis.*,sum(data) as total,user.username as username FROM `userhis` left join user on userhis.uid=user.id where type=4 and data>0 ".$con." GROUP by uid order by total desc ";
//        echo $sql;die;
        $rechangelist = Db::query($sql);
        $this->assign("datemin",$datemin);
        $this->assign("datemax",$datemax);
        $this->assign("list",$rechangelist);
        return view();
    }

    public function member_result(){
        $datemin = input('post.datemin','');
        $datemax = input('post.datemax','');

        $con = '';
        $this->assign("datemin",$datemin);
        $this->assign("datemax",$datemax);


        $maxtime = strtotime($datemax);
        if ($datemin && $datemin){
            if ($datemax == date('Y-m-d')){
//                $datemax = date("Y-m-d",strtotime("+1 day"));
                $maxtime = strtotime($datemax." 23:59:59");
            }
            $con = ' and rewards.create_time between  '. strtotime($datemin) .'    and  '. $maxtime.'     ';
        }

        //打赏合计
        $sql1 = "select tmp.*,sum(tmp.score) as score from 
                (select rewards.*,calToScore(reward,kid,direction,kou) as score,user.name,user.username from rewards left JOIN user on rewards.uid = user.id  
                left join game on rewards.gid=game.id 
                where 1=1 and game.winner!=0 ".$con."
                ) tmp  
                group by uid order by score desc";
        $rewardlist = Db::query($sql1);

        $sql2 = "select rewards.*,sum(win) as wintotal,user.username,user.name from rewards left JOIN user on rewards.uid = user.id WHERE 1=1 ".$con." GROUP by uid ORDER by wintotal desc";
        $winlist = Db::query($sql2);
        $sql3 = "select rewards.*,sum(lose) as losetotal,user.username,user.name from rewards left JOIN user on rewards.uid = user.id WHERE 1=1 ".$con."  GROUP by uid ORDER by losetotal desc";
        $loselist = Db::query($sql3);


        //平台收益
        $sql4 = "select sum(tax) as taxtotal from rewards where  1  ".$con;
        echo $sql4;
        $taxtotal = Db::query($sql4);
        if ($taxtotal[0]['taxtotal'] == null) $taxtotal[0]['taxtotal'] = 0;
        $this->assign('taxtotal',$taxtotal[0]['taxtotal'] );

        //用户收益统计
        $sql5 = "select rewards.*,sum(tax) as taxtotal,user.username,user.name from rewards left JOIN user on rewards.uid = user.id WHERE 1=1 ".$con."  GROUP by uid ORDER by taxtotal desc";
        $taxlist = Db::query($sql5);


        $this->assign("rewardlist",$rewardlist);
        $this->assign("winlist",$winlist);
        $this->assign("loselist",$loselist);
        $this->assign('taxlist',$taxlist);

        return view();
    }

    public function member_add()
    {
        $user = new \app\admin\model\User();
        if ($id = input('param.id')){
            $user = $user::get($id);
            $this->assign("user",$user);
        }

        if ($arr = input("post.")){

            if ($user->user_add($arr)){
                return ['status'=>1];
            }else{
                return ['status'=>0];
            }
        }
        $this->assign("password",rand(100000,999999));
        return view();
    }


  	public function member_del($id){
    	$user = \app\admin\model\User::get($id);
      	if($user->delete()){
        	return json(['status'=>1]);
        }else{
        	return json(['status'=>0,'error'=>'服务器错误']);
        }
    }

    public function member_withdrawal($uid)
    {
        $user = \app\admin\model\User::get($uid);
        if ($score = input('post.score')){
            if ( $user['score'] > $score){
                //提现
                $user->setDec('score',$score);

                //用户日志
                $userhis = new Userhis();
                $userhis['type'] = 5;
                $userhis['gid'] = 0;
                $userhis['uid'] = $uid;
                $userhis['data'] = 0 - $score;
                $userhis['create_time'] = date("Y-m-d H:i:s");
                $userhis['comment'] = "提现".$score;
                $userhis->save();

                //系统日志
                $log = new Log();
                $log['content'] = $user['username']."提现".$score;
                $log['opertor'] = "admin";
                $log['create_time'] = date("Y-m-d H:i:s");
                $log->save();

                return json(['status'=>1,'data'=>$user->getLastSql()]);
            }else{
                return json(['status'=>0,'error'=>'用户余额不足']);
            }
        }
        $this->assign('user',$user);
        return view();
    }

    public function recharge($id)
    {
        $user = \app\admin\model\User::get($id);
        if ($score = input("post.score")){
            $rs = $user->setInc('score',$score);
            if ($rs){
                $log = new Log();
                $log['content'] = $user['username']."充值".$score;
                $log['create_time'] = date("Y-m-d H:i:s");
                $log->save();

                $his = new Userhis();
                $his['gid'] = 0;
                $his['uid'] = $id;
                $his['type'] = 4;
                $his['data'] = $score;
                $his['create_time'] = date("Y-m-d H:i:s");
                $his['comment'] = "充值".$score;
                $his->save();

                return 1;
            }else{
                return 0;
            }
        }
        $this->assign("user",$user);
        return view();
    }

    public function member_show()
    {
        $user = \app\admin\model\User::get(input("param.id"));
        $this->assign("user",$user);
        return view();
    }

    public function change_password($id)
    {
        $user = \app\admin\model\User::get($id);
        if ($arr = input("post.")){
            $rs = $user->save(['password'=>md5(input('post.newpassword')),'id'=>$id]);
            if ($rs){
                return 1;
            }else{
                return 0;
            }
        }
        $this->assign("user",$user);
        return view();
    }

    public function member_stop($id)
    {
        $user = \app\admin\model\User::get($id);
        return $user->save(['status'=>0,'id'=>$id]);
    }

    public function member_enable($id)
    {
        $user = \app\admin\model\User::get($id);
        return $user->save(['status'=>1,'id'=>$id]);
    }

    public function member_his($uid, $type=null)
    {



        $user = \app\admin\model\User::get($uid);
        $this->assign("user",$user);
        $userhis = new Userhis();


            if ($type == 'recharg'){
                $his = $userhis->where(['uid'=>$uid,'type'=>4])->order('id','desc')->select();
            }else{
                $his = $userhis->where('uid',$uid)->order('id','desc')->select();
            }


        $this->assign("his",$his);
        return view();
    }



}